<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>幻灯片开发</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        div {
            position: relative;
            width: 300px;
            height: 200px;
            margin: 150px auto;
            border: 3px solid black;
            overflow: hidden;
        }

        ul {
            position: absolute;
            width: 1500px;
            height: 200px;
            list-style: none;
            transition: all 0.5s ease 0s;
        }

        li {
            float: left;
            width: 300px;
            height: 200px;
            background-color: red;
            font-size: 26px;
            text-align: center;
            line-height: 200px;
        }

        li:nth-child(2n) {
            background-color: yellow;
        }
    </style>
</head>

<body>
    <div id="lanternSlide">
        <ul id="list">
            <li>1</li>
            <li>2</li>
            <li>3</li>
            <li>4</li>
        </ul>
    </div>
    <script>
        var list = document.getElementById('list');
        var page = 0;
        let lock=true;
        class gu {
            constructor() {
                const li = list.firstElementChild.cloneNode(true);
                list.appendChild(li);
            }
            right() {
                if(!lock)return;
                lock=false;
                list.style.transition=" all 0.5s ease 0s";
                page++;
                if (page > 3) {
                    setTimeout(()=>{
                        list.style.transition='none'
                        page = 0;
                        list.style.left = `${-300 * page}px`;
                    },500)
                }
                list.style.left = `${-300 * page}px`;
                setTimeout(()=>{
                    lock=true;
                },500)
            }
            left() {
                if(!lock)return;
                lock=false;
                page--;
                if (page <0) {
                    list.style.transition='none'
                    page=4;
                    list.style.left = `${-300 * page}px`;
                    setTimeout(()=>{
                        list.style.transition=" all 0.5s ease 0s";
                        page = 3;
                        list.style.left = `${-300 * page}px`;
                    },1)
                }
                list.style.left = `${-300 * page}px`;
                setTimeout(()=>{
                    lock=true;
                },500)
            }
        }
        class mouse extends gu {
            constructor() {
                super();
                this.rui();
            }
            
            rui() {
                document.onkeyup = (e)=> {
                    e.preventDefault();
                    if (e.keyCode ==37) {
                        this.left();
                    }
                    if (e.keyCode == 39) {
                        clearInterval(time)
                        this.right();
                    }

                }
            }

        }
        var p = new mouse()
       var time= setInterval(()=>{
            p.right();
        },2000)

        
    </script>
</body>

</html>